package com.xckj.cycling.adapter

import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.widget.AppCompatImageView
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager.LayoutParams
import com.bumptech.glide.Glide

class ImageAdapter(private val list: List<String>?) : PagerAdapter() {
    override fun getCount(): Int {
        return list?.size ?: 0
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val img = AppCompatImageView(container.context)
        Glide.with(container).load(list?.get(position)).into(img)
        container.addView(img, LayoutParams())
        img.scaleType = ImageView.ScaleType.CENTER_CROP
        return img
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }
}